package com.itlong.cloud.rabbitmq.consumer;

import com.itlong.cloud.POJO.PO.TimeTaskDeadMessageQueuePO;
import com.itlong.cloud.app.service.IAppDeviceService;
import com.itlong.cloud.constants.RabbitMQConstant;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.node.JsonUtil;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <desc>
 *      接收app用户id及项目id，更新用户的设备缓存信息
 * </desc>
 *
 * @createDate 2017/11/13
 */
@Component
public class UpdateAppUserDevicesInfoConsumer {

    private static final Logger LOG = LoggerFactory.getLogger(UpdateAppUserDevicesInfoConsumer.class);

    @Autowired
    IAppDeviceService iAppDeviceService;


    /**
     * <desc>
     *      接收app用户id及项目id，更新用户的设备缓存信息
     * <desc/>
     *
     * @param   message 消息数据
     * @author Jiaqi.X
     * @createDate 2017/11/13
     */
    @RabbitListener(queues = RabbitMQConstant.RABBITMQ_EQUIP_APP_USER_INFO_DEVICE_UPDATE_QUEUE)
    public void process(Message message, Channel channel) throws IOException {
        byte[] body = message.getBody();
        try {
            TimeTaskDeadMessageQueuePO po= JsonUtil.toObject(new String(body), TimeTaskDeadMessageQueuePO.class);
            Map<String,Object> map=JsonUtil.toObject(po.getMessageContent(),HashMap.class);
//            Map<String,Object> map=JsonUtil.toObject(new String(message.getBody()),HashMap.class);
            iAppDeviceService.updateUserDevicesInfoCache(StringHandlerUtil.getNullOrStringForObject(map.get("projectId")),
                    StringHandlerUtil.getNullOrStringForObject(map.get("userId")),
                    StringHandlerUtil.getNullOrStringForObject(map.get("data")),
                    StringHandlerUtil.getNullOrStringForObject(map.get("userIdList")) != null ?
                            StringHandlerUtil.getNullOrStringForObject(JsonUtil.toJSON(map.get("userIdList"))) : null);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
            throw new DataAccessException("【RabbitMQ_接收app用户id及项目id，更新用户的设备缓存信息】:处理异常"+new String(body), e);
        }finally {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            LOG.info("RabbitMQ_接收app用户id及项目id，更新用户的设备缓存信息:" + new String(body));
        }
    }

    public static void main(String[] args) throws IOException {
        Map<String,Object> map=JsonUtil.toObject("{\"userIdList\":[{\"deviceAuth\":\"88888788777\",\"projectId\":\"00000660\",\"userId\":\"02acd8357eb728cf956c12a80c32ce86\"}],\"correlationId\":\"CorrelationData [id=65aceb1d-e7fd-4bba-bede-b59da3a5767f]\"}",HashMap.class);
        System.out.println(StringHandlerUtil.getNullOrStringForObject(JsonUtil.toJSON(map.get("userIdList"))));
    }
}